package com.example.pinkcat.activity;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.Save_set_Msg;
import com.example.pinkcat.activity.pinkCatUser.UserMsg;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;

import java.util.Objects;

public class LoginRegisterPage extends AppCompatActivity {
    Context context=this;

    EditText edit_account;//账号
    EditText edit_password;//密码或验证码

    TextView login_way_TextView;
    TextView getForget_Verification;
    TextView btn_change_login_way;

    String phone;
    String mail;
    String password;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
        //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_login_register_page);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);//允许此线程内发起网络请求


        //
        edit_account=findViewById(R.id.edit_account);
        edit_password=findViewById(R.id.edit_password);
        login_way_TextView=findViewById(R.id.text_password);
        getForget_Verification=findViewById(R.id.forget_password);
        btn_change_login_way=findViewById(R.id.change_login_way);

        //初始化登陆类型
        login_way_TextView.setText(R.string.passwrod);
        edit_password.setHint(R.string.plz_inputPassword);
        edit_password.setInputType(129);
        getForget_Verification.setText(R.string.forgetpassword);
        btn_change_login_way.setText(R.string.verificationlogin);

        //监听
        listener();

    }


    Handler handler=new Handler(new Handler.Callback() {
        @SuppressLint("SetTextI18n")
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what==250)
                getForget_Verification.setText(R.string.get_verification);
            else
                getForget_Verification.setText(msg.what+"s");
            return true;
        }
    });

    void listener(){
        //登陆方式
        btn_change_login_way.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btn_change_login_way.getText().toString().equals("密码登陆")){
                    login_way_TextView.setText(R.string.passwrod);
                    edit_password.setHint(R.string.plz_inputPassword);
                    edit_password.setInputType(129);
                    getForget_Verification.setText(R.string.forgetpassword);
                    btn_change_login_way.setText(R.string.verificationlogin);
                }
                else {
                    login_way_TextView.setText(R.string.verification_code);
                    edit_password.setHint(R.string.plz_verification_code);
                    edit_password.setInputType(InputType.TYPE_CLASS_NUMBER);
                    getForget_Verification.setText(R.string.get_verification);
                    btn_change_login_way.setText(R.string.passwordlogin);
                }
            }
        });

        //登陆监听
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.progressBar_wait).setVisibility(View.VISIBLE);
                phone=edit_account.getText().toString();
                mail=edit_account.getText().toString();
                password=edit_password.getText().toString();
                if (phone==null||phone.length()<2||password.length()<2){
                    Toast.makeText(context,"请输入账号或者密码", Toast.LENGTH_SHORT).show();
                    findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                    return;
                }
                if (btn_change_login_way.getText().toString().equals("密码登陆")){
                    if (GetData.longin(phone,mail,password,context,true)){
                        GetData.getUserinfo(context);//获取用户信息

                        //GetData.getUserinfo(context);
                        //保存必要信息到本地文件
                        UserMsg userMsg=new UserMsg();
                        userMsg.userId= userStaticMsg.userId;//-1
                        userMsg.userName=userStaticMsg.userName;
                        userMsg.phone=userStaticMsg.phone;
                        userMsg.mail=userStaticMsg.mail;
                        userMsg.head_img =userStaticMsg.head_img;
                        userMsg.login=userStaticMsg.login;
                        userMsg.vip=userStaticMsg.vip;
                        userMsg.style=userStaticMsg.style;
                        userMsg.appVersion=userStaticMsg.appVersion;
                        userMsg.appUrl=userStaticMsg.appUrl;
                        userMsg.JSESSIONID=userStaticMsg.JSESSIONID;
                        userMsg.password=userStaticMsg.password;
                        Save_set_Msg.Save(userMsg,context);

                        findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                        //执行更新状态并返回
                        onBackPressed();
                    }
                }
                else {
                    if (GetData.longin(phone,mail,password,context,false)){
                        GetData.getUserinfo(context);//获取用户信息

                        //GetData.getUserinfo(context);
                        UserMsg userMsg=new UserMsg();
                        userMsg.userId= userStaticMsg.userId;//-1
                        userMsg.userName=userStaticMsg.userName;
                        userMsg.phone=userStaticMsg.phone;
                        userMsg.mail=userStaticMsg.mail;
                        userMsg.head_img =userStaticMsg.head_img;
                        userMsg.login=userStaticMsg.login;
                        userMsg.vip=userStaticMsg.vip;
                        userMsg.style=userStaticMsg.style;
                        userMsg.appVersion=userStaticMsg.appVersion;
                        userMsg.appUrl=userStaticMsg.appUrl;
                        userMsg.JSESSIONID=userStaticMsg.JSESSIONID;
                        userMsg.password=userStaticMsg.password;
                        Save_set_Msg.Save(userMsg,context);

                        findViewById(R.id.progressBar_wait).setVisibility(View.INVISIBLE);
                        //执行更新状态并返回
                        onBackPressed();
                    }
                }
            }
        });

        //返回监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //注册监听
        findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,RegisterPage.class);
                startActivity(intent);
            }
        });

        //更改密码监听或者获取验证码
        findViewById(R.id.forget_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login_way_TextView.getText().toString().equals("密码")){//当前为密码登陆测可以有忘记密码
                    Intent intent=new Intent(context,ChangePasswordPage.class);
                    startActivity(intent);
                }
                else {//当前为验证码登陆测可以有获取验证码
                    if (edit_account.getText().toString().length()<2){
                        Toast.makeText(context,"请输入正确的账号信息",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    GetData.getVerification(edit_account.getText().toString(),edit_account.getText().toString(),context);
                    new Thread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            getForget_Verification.setClickable(false);
                            getForget_Verification.setTextColor(getResources().getColor(R.color.black_overlay));
                            int count=60;
                            while (count>0){
                                try {
                                    Message message = new Message();
                                    message.what = count;
                                    handler.sendMessage(message);
                                    Thread.sleep(1000);
                                    count--;
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            Message message = new Message();
                            message.what = 250;
                            handler.sendMessage(message);
                            getForget_Verification.setTextColor(getResources().getColor(R.color.pink));
                            getForget_Verification.setClickable(true);
                        }
                    }).start();
                }
            }
        });

        //用户协议
        findViewById(R.id.useragreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,PinCatAgreement.class);
                intent.putExtra("agreementTitle","用户协议");
                startActivity(intent);
            }
        });
        //隐私协议
        findViewById(R.id.privacy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,PinCatAgreement.class);
                intent.putExtra("agreementTitle","隐私协议");
                startActivity(intent);
            }
        });

        //点击输入账号监听
        ImageView loginImage=findViewById(R.id.loginimage);
        edit_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                loginImage.setImageResource(R.mipmap.close_eyes);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edit_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                loginImage.setImageResource(R.mipmap.open_eyes);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.putExtra("sadas","asdas");
        setResult(115,intent);
        super.onBackPressed();
    }
}